package com.glodon.paas.document.storage.web;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.glodon.paas.document.storage.service.AuditService;

@Controller
@RequestMapping("/audit")
public class AuditController {
    
    private DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    @Value("${storage.bucketName}")
    private String bucketName;
    
    @Autowired
    private AuditService auditService;
    
    @RequestMapping(value = "/{date}")
    public void getAudit(@PathVariable String date, HttpServletRequest request, Map<String, Object> model) throws ParseException {
        if (request.getParameter("add") != null) {
            model.put(bucketName, getAddAudit(date));
        } else if(request.getParameter("delete") != null) {
            model.put(bucketName, getDeleteAudit(date));
        }
    }

    private List<String> getDeleteAudit(String date) throws ParseException {
        return auditService.getAuditObjectId("delete", dateFormat.parse(date));
    }

    private List<String> getAddAudit(String date) throws ParseException {
        return auditService.getAuditObjectId("add", dateFormat.parse(date));
    }
}
